var app = getApp()
Page({
   data: {
      contents: {},
      array1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      viphomework_title: '',
      userInfo: {},
      pickerHidden: true,
      chosen: ''

   },
   // 页面初始化 res为页面跳转所带来的参数
   onLoad: function (res) {

      console.log('传递过来的作业id：', res.id)
      console.log('传递过来的作业viphomeworkid：', res.viphomeworkid)
      console.log('传递过来的作业open_id：', res.open_id)
      //console.log('传递过来的作业title：', res.viphomework_title)
      getApp().globalData.id = res.id
      getApp().globalData.viphomeworkid = res.viphomeworkid
      getApp().globalData.open_id = res.open_id

      console.log('全局open_id：', getApp().globalData.open_id)

      getApp().globalData.viphomework_title = res.viphomework_title
      console.log('传递过来的作业title：', res.viphomework_title)

      var that = this;

      //调用应用实例的方法获取全局数据
      // app.getUserInfo(function (userInfo) {
      //    //更新数据
      //    that.setData({
      //       userInfo: userInfo
      //    })
      //    that.update()
      //    // console.log("nickName:" + userInfo.nickName);
      //    // console.log("avatarUrl:" + userInfo.avatarUrl);
      // })


      wx.request({
         url: 'https://edu.aijinrun.com/wx_request_api_viptopiclist.php',
         data: {
            id: res.id,
            viphomeworkid: res.viphomeworkid,
            open_id: res.open_id
         },
         method: 'GET', // res, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
         header: {
            'Content-Type': 'application/json'
         },
         // 页面初始化 res为页面跳转所带来的参数
         success: function (res) {
            console.log('这里是wx_request_api_viptopiclist.php返回的数据：', res)
            that.setData({
               contents: res.data,
               viphomework_title: getApp().globalData.viphomework_title,


            })
         }

      })

   },
   tapToAuthorize: function () {
      //再授权
      wx.openSetting({
         success: (res) => {
            /*
             * res.authSetting = {
             *   "scope.userInfo": true,
             *   "scope.userLocation": true
             * }
             */
            //因为openSetting会返回用户当前设置，所以通过res.authSetting["scope.userInfo"]来判断用户是否勾选了【用户信息】这一项
            if (res.authSetting["scope.userInfo"] === true) {
               var that = this
               app.getUserInfo(function (userInfo) {
                  //更新数据
                  that.setData({
                     userInfo: userInfo,
                     noAuthorized: false
                  })
               })
            }
            else {
               wx.showModal({
                  title: '用户未授权',
                  content: '如需正常使用小程序，请点击授权按钮，勾选用户信息并点击确定。',
                  showCancel: false,
                  success: function (res) {
                     if (res.confirm) {
                        console.log('用户点击确定')
                     }
                  }
               })
            }
         }
      })
   },

   pickerConfirm: function (e) {
      this.setData({
         pickerHidden: true
      })
      this.setData({
         chosen: e.detail.value
      })
   },
   pickerCancel: function (e) {
      this.setData({
         pickerHidden: true
      })
   },
   pickerShow: function (e) {
      this.setData({
         pickerHidden: false
      })
   },
   formSubmit: function (e) {
      console.log('form发生了submit事件，携带数据为：', e.detail.value, '全局open_id：', getApp().globalData.open_id)
      var that = this;
      wx.showModal({
         title: '提示',
         content: '您确定要提交作业吗？',
         success: function (res) {
            if (res.confirm) {
               console.log('用户点击了确定提交')
               wx.request({
                  url: 'https://edu.aijinrun.com/wx_request_api_subviphomework-xw.php',
                  header: { "Content-Type": "application/x-www-form-urlencoded" },
                  method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                  data: {
                     id: getApp().globalData.id,
                     open_id: getApp().globalData.open_id,
                     viphomeworkid: getApp().globalData.viphomeworkid,
                     homeworkArray: e.detail.value,
                     evaContent: e.detail.evaContent
                  },
                  // 页面初始化 res为页面跳转所带来的参数
                  success: function (res) {
                     console.log('这里是wx_request_api_subhomework.php返回的数据：', res)
                     that.setData({
                        homeworkrows: res.data,

                     })

                     getApp().globalData.homeworkrows = res.data
                     // console.log("该用户是否已提交过作业:", res.data);
                     console.log("该用户是否已提交过作业:", getApp().globalData.homeworkrows);

                     if (getApp().globalData.homeworkrows == 1) {
                        wx.showModal({
                           title: "提示",
                           content: "您已提交过该作业",
                           showCancel: false,
                           confirmText: "确定"
                        })
                     } else {
                        wx.showToast({
                           title: '提交作业成功',
                           icon: 'success',
                           duration: 2000,
                           success: function () {
                              console.log('提交作业成功');
                              setTimeout(function () {
                                 //要延时执行的代码
                                 wx.navigateTo({
                                    url: '../viphomeworkcat/viphomeworkcat'
                                 })
                              }, 2000) //延迟时间
                           }
                        })
                     }

                  }
               })

            } else if (res.cancel) {
               console.log('用户点击了取消提交')
            }
         }
      })

   },
   formReset: function (e) {
      console.log('form发生了reset事件，携带数据为：', e.detail.value)
      this.setData({
         chosen: ''
      })
   }
})